	package com.insightg3.android;

import java.util.ArrayList;
import java.util.List;

import com.google.api.translate.Language;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class CompareResultActivity extends Activity {

	private ArrayList<SearchResultStatus> list = null;
	private SearchResultAdapter adapter = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main_result_listview);
		
		ListView listview = (ListView) findViewById(R.id.lvResult);
		
		List<SearchResultStatus> selectedItems = new ArrayList<SearchResultStatus>();
		selectedItems = ((AndroidApplication)getApplication()).getSelectedItems();
		
		Log.d("selected items is :", selectedItems.toString());

		list = new ArrayList<SearchResultStatus>();
		if(selectedItems == null || selectedItems.isEmpty()){
		}
		else{
			list = (ArrayList<SearchResultStatus>)selectedItems;
		}
		
		SearchResultAdapter adapter = new SearchResultAdapter(this, R.layout.main_result_row, list);

		listview.setAdapter(adapter);

		listview.setOnItemClickListener(new OnItemClickListener() {  
			@Override  
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
			long arg3) {  
				setTitle("Click"+arg2+" Item");  
			}  
			});  
		
		listview.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {  

			@Override  
			public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
			//menu.setHeaderTitle("LongTouch-ContextMenu");     
			menu.add(0, 0, 0, "Remove From List");  
			menu.add(0, 1, 0, "Clear All");     
			}  
			});   
	}


	@Override  
    public boolean onContextItemSelected(MenuItem item) { 
	       setTitle("Long touch "+item.getItemId()+" Item");  
	       ListView listview = (ListView) findViewById(R.id.lvResult);

	       switch(item.getItemId()){
	       case 0:
	           int size = listview.getChildCount();
	           for (int i = 0; i < size; i++) {
	              final View listDataView = listview.getChildAt(i);
	              CheckBox chkSelect = (CheckBox) listDataView.findViewById(R.id.chkSelect);
	              if (chkSelect.isChecked()) {
	                  ((AndroidApplication)getApplication()).removeSelectedItem(list.get(i));
	              }
	           }
	           break;
	       case 1:
	           ((AndroidApplication)getApplication()).setSelectedItems(null);
	           break;
	       }
	       List<SearchResultStatus> selectedItems = ((AndroidApplication)getApplication()).getSelectedItems();
	       //Log.d("selected items is :", selectedItems.toString());
	       list = new ArrayList<SearchResultStatus>();

	       if(selectedItems == null || selectedItems.isEmpty()){
	       }
	       else{
	           list = (ArrayList<SearchResultStatus>)selectedItems;
	       }
	       adapter = new SearchResultAdapter(CompareResultActivity.this, R.layout.main_result_row, list);
	       listview.setAdapter(adapter);
	       return super.onContextItemSelected(item); 
	    } 

}
